//Guard Module - Detection zone faces away from planet

//Adds the other functions in the guard module
//To a given Entity object
function addGuardProperties(obj, radius, angle, lookAngle, detectionBlock){
	obj.drawGuard = drawGuard;
	obj.checkDetection = checkDetection;
	obj.drawDetection = drawDetection;
	obj.detectionRadius = radius;
	obj.detectionAngle = angle;
	obj.lookAngle = lookAngle;
	obj.detectionBlock = detectionBlock;
	obj.detectionBlocked = detectionBlocked;
	if (detectionBlock) obj.detectionColor = "lime";
	else obj.detectionColor = "white";
	return obj;
}

drawGuard = function(){
	this.drawDetection();
	this.drawEntity(); //draws the detection underneath the sprite
};

//Only checks the center of the player's sprite at the moment
checkDetection = function(object) {
	if (Math.sqrt((object.pos.x-this.pos.x)*(object.pos.x-this.pos.x) +
		(object.pos.y-this.pos.y)*(object.pos.y-this.pos.y)) > this.detectionRadius) return false;
	
	var xpos1 = Math.sin(this.angle+this.lookAngle-this.detectionAngle)*(this.detectionRadius/Math.cos(this.detectionAngle)*5000);
	var ypos1 = Math.cos(this.angle+this.lookAngle-this.detectionAngle-Math.PI)*(this.detectionRadius/Math.cos(this.detectionAngle)*5000);

	var xpos2 = Math.sin(this.angle+this.lookAngle+this.detectionAngle)*(this.detectionRadius/Math.cos(this.detectionAngle)*5000); 
	var ypos2 = Math.cos(this.angle+this.lookAngle+this.detectionAngle-Math.PI)*(this.detectionRadius/Math.cos(this.detectionAngle)*5000);

	if ((((this.pos.x - xpos1)*(object.pos.y - ypos1) - (this.pos.y - ypos1)*(object.pos.x - xpos1)) < 0) &&
		(((this.pos.x - xpos2)*(object.pos.y - ypos2) - (this.pos.y - ypos2)*(object.pos.x - xpos2)) > 0)) {
		if (this.detectionBlock) {
			if (this.detectionBlocked()) return false;
			return true;
		} 
		
		return true;
	} 
	
	return false;
};

drawDetection = function() {
	if((this.pos.x > player.pos.x - canvas.width/2 - this.width - this.detectionRadius &&
		this.pos.x < player.pos.x + canvas.width/2 + this.width + this.detectionRadius) &&
		(this.pos.y > player.pos.y - canvas.height/2 - this.height - this.detectionRadius &&
		this.pos.y < player.pos.y + canvas.height/2 + this.height + this.detectionRadius)) {

		var xthis = this.pos.x;
		var ythis = this.pos.y;
	
		//draw two sides of the "triangle"
		var xpos = Math.sin(this.angle-this.detectionAngle+this.lookAngle)*(this.detectionRadius); 
		var ypos = Math.cos(this.angle-this.detectionAngle+this.lookAngle-Math.PI)*(this.detectionRadius);
		ctx.beginPath();
		ctx.moveTo(xthis, ythis);
		ctx.strokeStyle = this.detectionColor;
		ctx.lineWidth = 1;
		ctx.lineTo(xthis + xpos, ythis + ypos);
		ctx.stroke();
	
		xpos = Math.sin(this.angle+this.detectionAngle+this.lookAngle)*(this.detectionRadius);
		ypos = Math.cos(this.angle+this.detectionAngle+this.lookAngle-Math.PI)*(this.detectionRadius);
		ctx.beginPath();
		ctx.moveTo(xthis, ythis);
		ctx.strokeStyle = this.detectionColor;
		ctx.lineWidth = 1;
		ctx.lineTo(xthis + xpos, ythis + ypos);
		ctx.stroke();

		//draw arc
		ctx.beginPath();
		ctx.lineWidth = "1";
		ctx.strokeStyle = this.detectionColor;
		ctx.arc(xthis, ythis, this.detectionRadius, this.angle-this.detectionAngle+this.lookAngle-Math.PI/2, this.angle+this.detectionAngle+this.lookAngle-Math.PI/2);
		ctx.stroke();
	}
};

//Checks to see if an obstacle is between the player and this guard
//Works by checking to see if the four corners of the obstacle sprite are on the same side of the line-of-sight line
detectionBlocked = function() {
	var dist = Math.sqrt((player.pos.x-this.pos.x)*(player.pos.x-this.pos.x) + (player.pos.y-this.pos.y)*(player.pos.y-this.pos.y));

	for (var i = 0; i < obstacles.length; i++) {
		if ((Math.sqrt((obstacles[i].pos.x-this.pos.x)*(obstacles[i].pos.x-this.pos.x) + (obstacles[i].pos.y-this.pos.y)*(obstacles[i].pos.y-this.pos.y)) < dist) &&
			(Math.abs(obstacles[i].pos.x-this.pos.x) > this.width) &&
			(Math.abs(obstacles[i].pos.y-this.pos.y) > this.height)) {
			
			//rotate obstacle hitbox
			var sobj = Math.sin(obstacles[i].angle);
			var cobj = Math.cos(obstacles[i].angle);
		
			var objcorners = [{x: obstacles[i].pos.x-obstacles[i].width/2-2, y: obstacles[i].pos.y-obstacles[i].height/2-2},
							  {x: obstacles[i].pos.x+obstacles[i].width/2+2, y: obstacles[i].pos.y-obstacles[i].height/2-2},
							  {x: obstacles[i].pos.x-obstacles[i].width/2-2, y: obstacles[i].pos.y+obstacles[i].height/2+2},
							  {x: obstacles[i].pos.x+obstacles[i].width/2+2, y: obstacles[i].pos.y+obstacles[i].height/2+2}];
			
			var nobjcorners = [{x: 0, y: 0}, {x: 0, y: 0}, {x: 0, y: 0}, {x: 0, y: 0}];

			for (var j = 0; j < 4; j++) {
				nobjcorners[j].x = objcorners[j].x*cobj-objcorners[j].y*sobj;
				nobjcorners[j].y = objcorners[j].x*sobj+objcorners[j].y*cobj;
			}

			
			
			//translate coords so guard is at origin
			for (var j = 0; j < 4; j++) {
				objcorners[j].x = nobjcorners[j].x-this.pos.x;
				objcorners[j].y = nobjcorners[j].y-this.pos.y; 
			}
			
			
			//rotate so the player ship is vertical
			var sinpg = (player.pos.x-this.pos.x)/(dist);
			var cospg = (player.pos.y-this.pos.y)/(dist);
			
			for (var j = 0; j < 4; j++) {
				nobjcorners[j].x = objcorners[j].x*cospg-objcorners[j].y*sinpg;
				//nobjcorners[j].y = objcorners[j].x*sinpg+objcorners[j].y*cospg;
			}

			
			//check sign of x coord for each corner of the obstacle
			if (!(
				(nobjcorners[0].x < 0) === (nobjcorners[1].x < 0) &&
				(nobjcorners[1].x < 0) === (nobjcorners[2].x < 0) &&
				(nobjcorners[2].x < 0) === (nobjcorners[3].x < 0) &&
				(nobjcorners[3].x < 0) === (nobjcorners[0].x < 0)
				)) {
				return true;
			}
		}
	}

	return false;
}



